package utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 计算两个时间之间的间隔天数
 * 
 * @author acer
 * 
 */
public class CalculateDate {
	/**
	 * 得到几天前的时间
	 * 
	 * @param d 要计算的时间
	 * @param day 间隔天数
	 * @return  日期结果
	 */
	public static Date getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}
	/**
	 * 得到几天前的时间（从0点开始）
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateBeforeByDay(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MILLISECOND, 0);
		return now.getTime();
	}
	
	/**
	 * 得到几天后的时间
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}
     /**
      * 得到两个时间差的时分秒
      * @param timeInSeconds
      */
	public static String  calcHMS(long timeInSeconds) {
		long hours, minutes, seconds;
		// 1(hour)*60(minite)*60(seconds)*1000(millisecond)
		hours = timeInSeconds / 3600000;
		timeInSeconds = timeInSeconds - (hours * 3600 * 1000);
		// 1(seconds)*1000(millisecond)
		minutes = timeInSeconds / 60000;
		timeInSeconds = timeInSeconds - (minutes * 60 * 1000);
		// 1(seconds)*1000(millisecond)
		seconds = timeInSeconds / 1000;
		return hours + " hour(H) " + minutes + " minute(M) "
				+ seconds + " second(S)";
	}

	/**
	 * 计算两个时间间隔
	 * 
	 * @param timeInSeconds
	 *            两个时间相差Long值数
	 * @return xx天xx小时xx分xx秒
	 */
	public static String canageDHMS(long timeInSeconds) {
		long days, hours, minutes, seconds;
		// 1(day)*24(hour)*60(minite)*60(seconds)*1000(millisecond)
		days = timeInSeconds / 86400000;
		timeInSeconds = timeInSeconds - (days * 3600 * 24 * 1000);
		// 1(hour)*60(minite)*60(seconds)*1000(millisecond)
		hours = timeInSeconds / 3600000;
		timeInSeconds = timeInSeconds - (hours * 3600 * 1000);
		// 1(seconds)*1000(millisecond)
		minutes = timeInSeconds / 60000;
		timeInSeconds = timeInSeconds - (minutes * 60 * 1000);
		// 1(seconds)*1000(millisecond)
		seconds = timeInSeconds / 1000;
		String day = days == 0 ? "" : days + "天";
		String hour = days != 0 ? hours + "小时" : hours == 0 ? "" : hours + "小时";
		String minu = seconds != 0 ? minutes == 0 ? "" : minutes + "分"
				: minutes + "分";
		String sec = seconds == 0 ? "" : seconds + "秒";
		return day + hour + minu + sec;
	}

	/**
	 * 计算时间间隔天数
	 * 
	 * @param startdate
	 *            开始时间
	 * @param enddate
	 *            结束时间
	 * @return
	 */
	public static Integer getDayOfDate(Date startdate, Date enddate) {
		return (int) ((enddate.getTime() - startdate.getTime()) / (3600 * 24 * 1000));
	}
	/**
	 * 获取滞留时间
	 * @param startdate
	 * @param enddate
	 * @return
	 */
	public static int getLeftTime(Date startdate, Date enddate) {
		Calendar now = Calendar.getInstance();
		Calendar before = Calendar.getInstance();
		now.setTime(enddate);
		before.setTime(startdate);
		return now.get(Calendar.DAY_OF_YEAR)-before.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 根据(,)号分解字符串.
	 * 
	 * @param param
	 *            要分解的字符串
	 * @param split
	 *            分隔符
	 * @return 分解后的整型数组
	 */
	public static Integer[] getIntegerList(String param, String split) {
		Integer[] array = null;
		if (param != null && !param.isEmpty()) {
			String[] str = param.split(split);
			if (str.length > 0) {
				array = new Integer[str.length];
				for (int i = 0; i < str.length; i++) {
					try {
						array[i] = Integer.parseInt(str[i]);
					} catch (Exception e) {
						System.out.println("数字转换异常");
						e.printStackTrace();
					}
				}
			}
		}
		return array;
	}

	/**
	 * 根据指定分隔符来分割字符串
	 * 
	 * @param param
	 *            要分解的字符串
	 * @param split
	 *            分隔符
	 * @return 分割后的返回值
	 */
	public static List<String> getStringList(String param, String split) {
		List<String> list = new ArrayList<String>();
		String[] str = param.split(split);
		for (String string : str) {
			list.add(string);
		}
		return list;
	}
	
	public static void main(String []args){
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		Calendar before = Calendar.getInstance();
		before.setTime(new Date());
		before.set(Calendar.DATE, before.get(Calendar.DATE) - 100);
		
		System.out.println(now.get(Calendar.DAY_OF_YEAR));
		System.out.println(before.get(Calendar.DAY_OF_YEAR));
	}
}
